<?php
/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2019/5/15
 * Time: 15:44
 */

namespace app\admin\controller;
use PHPExcel;
use PHPExcel_IOFactory;
use app\common\model\Site;
use app\admin\logic\UsersLogic;
use think\facade\Request;

class User extends Base {

    private $userLogic;

    public function __construct()
    {
        parent::__construct();
        $this->userLogic = new UsersLogic();
    }

    public function export(){
        $objPHPExcel=new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
// 设置列的宽度
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
// 设置表头
        $objPHPExcel->getActiveSheet()->SetCellValue('A1', '序号');
        $objPHPExcel->getActiveSheet()->SetCellValue('B1', '昵称');
        $objPHPExcel->getActiveSheet()->SetCellValue('C1', '姓名');
        $objPHPExcel->getActiveSheet()->SetCellValue('D1', '联系方式');
        $objPHPExcel->getActiveSheet()->SetCellValue('E1', '是否党员');
        $objPHPExcel->getActiveSheet()->SetCellValue('F1', '积分');
        $objPHPExcel->getActiveSheet()->SetCellValue('G1', '注册时间');

        $where = Request::param();
        if($where['type']=='all'){
            $where['limit'] = 100000;
        }
        $res = $this->userLogic->userList($where, $where['page']);
        $data = $res['data'];
        $num = 2;
        foreach ($data as $k => $v) {
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $num, ($k+1));
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $num, $v['nick_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $num, $v['user_name']);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $num, $v['mobile']);
            $objPHPExcel->getActiveSheet()->SetCellValue('E' . $num, $v['is_party']?'是':'否');
            $objPHPExcel->getActiveSheet()->SetCellValue('F' . $num, $v['integral']);
            $objPHPExcel->getActiveSheet()->SetCellValue('G' . $num, ' '.$v['create_time']);
            $num++;
        }
        $fileName = "积分兑换表" . date('Y-m-d', time()) . rand(1, 1000);
        $xlsName = iconv('utf-8', 'gb2312', $fileName);
        $objPHPExcel->getActiveSheet()->setTitle('sheet'); // 设置工作表名
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel); //下载 excel5与excel2007
        ob_end_clean(); // 清除缓冲区,避免乱码
        header("Pragma: public");
        header("Expires: 0");
        header("Cache-Control:must-revalidate,post-check=0,pre-check=0");
        header("Content-Type:application/force-download");
        header("Content-Type:application/vnd.ms-execl;charset=UTF-8");
        header("Content-Type:application/octet-stream");
        header("Content-Type:application/download");
        header("Content-Disposition:attachment;filename=" . $xlsName . ".xls");
        header("Content-Transfer-Encoding:binary");
        $objWriter->save("php://output");
    }

    public function index(){
        return $this->fetch();
    }

    public function site(){
        return $this->fetch();
    }

    public function read(){
        $this->assign('id',input('id'));
        return $this->fetch();
    }

    public function audit(){
        return $this->fetch();
    }

    public function ajaxRead(){
        $data = Request::param();
        $userList = $this->userLogic->userIntegral($data, self::$nowPage);
        return json($userList);
    }

    public function ajaxIndex(){
        $data = Request::param();
        $userList = $this->userLogic->userList($data, self::$nowPage);
        return json($userList);
    }

    public function ajaxSite(){
        $data = Request::param();
        $userList = $this->userLogic->userSiteList($data, self::$nowPage);
        return json($userList);
    }

    public function ajaxAudit(){
        $data = Request::param();
        $userList = $this->userLogic->auditList($data, self::$nowPage);
        return json($userList);
    }

    public function add(){
        $id = Request::get('id');
        $act = "edit";
        if ($id){
            $info = $this->userLogic->userInfo($id);
            $this->assign('info', $info['data']);
        }
        $this->assign('act', $act);
        return view();
    }

    public function handle(){
        $data = Request::request();
        $res = $this->userLogic->handle($data);
        return json($res);
    }

    public function audit_site(){
        $id = Request::get('id');
        $info = $this->userLogic->userInfo($id);
        $this->assign('info', $info['data']);
        $site = Site::select();
        $this->assign('site',$site);
        return view();
    }


    public function audit_info(){
        $id = Request::get('id');
        $info = $this->userLogic->auditInfo($id);
        $this->assign('info', $info['data']);
        return view();
    }


    public function auditHandle(){
        $data = Request::request();
        $res = $this->userLogic->auditHandle($data);
        return json($res);
    }
    public function siteHandle(){
        $data = Request::request();
        $res = $this->userLogic->siteHandle($data);
        return json($res);
    }

}